package com.juzi.wopu.web;

import java.io.File;
import java.util.List;
import java.util.UUID;

import com.juzi.wopu.domain.Employee;
import com.juzi.wopu.service.IEmployeeService;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

/**
 * 处理导入的功能
 * @author 园
 *
 */
public class ImportAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	private IEmployeeService employeeService;
	
	//private IDepartmentService departmentService;
	
	private String name;
	
	private File upload;
	
	private String  contentType;
	
	private String fileName;
	
	private int count = 0;
	
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
//	public void setDepartmentService(IDepartmentService departmentService) {
//		this.departmentService = departmentService;
//	}
	
	@Override
	@InputConfig(resultName=SUCCESS)// 当验证方法没有通过时,改变默认返回的input视图,这样不会再页面报错
	public String execute() throws Exception {
		if(upload != null){
			List<String[]> data=employeeService.importExcls(upload);
			for (String[] strings : data) {
				Employee employee=new Employee();
				employee.setUsername(strings[0]+UUID.randomUUID().toString());
				employee.setPassword(strings[1]);
				employee.setEmail(strings[2]);
				employee.setAge(Integer.parseInt(strings[3]));
				/*employee.setDepartment(departmentService.findByName(strings[4]));*/
				employeeService.save(employee);
			}
			putContext("msg", "成功导入"+data.size()+"条数据");	
		}
		return SUCCESS;
	}
	
	public String upload() throws Exception {
		return SUCCESS;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return contentType;
	}

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getUploadFileName() {
		return fileName;
	}

	public void setUploadFileName(String fileName) {
		this.fileName = fileName;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
}
